-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Printing String objects as strings instead of objects #1450
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's absurd to ever use boxed primitives in JavaScript; but that said, enzyme should certainly handle it well even if people do bad things.
The console has superpowers; if we want to detect boxed strings, we have to have special-cased code for it.
However, another alternative is to use the object-inspect
library for the "object" case, which already handles all boxed primitives, and might allow the Debug code to be greatly simplified.
packages/enzyme/src/Debug.js
Outdated
@@ -32,7 +32,11 @@ function propString(prop) { | |||
case 'boolean': | |||
return `{${prop}}`; | |||
case 'object': | |||
return '{{...}}'; | |||
if (prop instanceof String) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instanceof
is never reliable, and should not be used.
If we want to detect boxed strings, we need to use the is-string
library.
packages/enzyme/src/Debug.js
Outdated
@@ -32,7 +32,11 @@ function propString(prop) { | |||
case 'boolean': | |||
return `{${prop}}`; | |||
case 'object': | |||
return '{{...}}'; | |||
if (prop instanceof String) { | |||
return `"${prop.toString()}"`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String(x)
is always strictly better than x.toString()
, and inside a template literal, both are unnecessary for a non-Symbol - this can just be `"${prop}"`
@ljharb thanks for the feedback - just pushed some changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM pending one nit, and a rebase
packages/enzyme/src/Debug.js
Outdated
return '{{...}}'; | ||
if (isString(prop)) { | ||
return `"${prop}"`; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The else here isn’t necessary; the following return can be de-indented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 updated
7e2fd4f
to
8533b5e
Compare
This pull request updates the way that nodes are printed when being debugged.
Use case in question
Previous behavior
Debugging the above node previously logged the following:
Proposed behavior
I propose it would make more sense to print props that are String objects as strings:
Notes
Providing String objects for element props like className is something that is supported by the DOM, and thus supported by React.